<?php
namespace app\model\cms;
use think\Model;
use think\facade\Db;
class categoryModel extends Model{

    protected $table   = 'sh_cms_category';

    public static function get_list(){

        $sql = 'select c.*, '
            . ' group_concat(p.name order by path_id asc separator ">") as path_name, '
            . ' group_concat(p.id order by path_id asc separator ",") as path_id, '
            . ' group_concat(p.sort_order order by path_id asc separator ",") as sort_order_all '
            . ' from sh_cms_category c '
            . ' inner join sh_cms_category_path cp '
            . ' on cp.category_id =c.id'
            . ' inner join sh_cms_category p '
            . ' on p.id =cp.path_id'
            . ' group by c.id'
//            . ' order by path_id asc, c.sort_order desc'
            . ' order by sort_order_all asc'
        ;

//        echo $sql;
        // return pdo_fetchall( $sql );
        return Db::query( $sql );
    }

    public static function getCategoryByids( $ids ){

        $ids = is_array( $ids )? implode(',', $ids):$ids;
        $sql = 'select * from sh_cms_category where id in (' . $ids . ')';
        // return pdo_fetchall( $sql );
        return Db::query( $sql );
    }
    public static function single( $id ){

        $sql = 'select c.*, '
            . ' group_concat(path.name order by path_id asc separator ">") as path_name '
//            . ' p.name as parent_name '
            . ' from sh_cms_category c'
//            . ' left join sh_cms_category p '
//            . ' on p.id =c.parent_id'
            . ' inner join sh_cms_category_path cp '
            . ' on cp.category_id =c.id'
            . ' inner join sh_cms_category path '
            . ' on path.id =cp.path_id'
            . ' where c.id=' . $id;

        // return pdo_fetch( $sql );
        return Db::query( $sql )[0];
    }

    public static function children( $pid, $recursion=false ){

        $sql = 'select * from sh_cms_category where parent_id=' . $pid;

        // $list = pdo_fetchall( $sql );
        $list = Db::query( $sql );
        if( $recursion ){
            foreach( $list as &$cat ){
                $children = self::children( $cat['id'], $recursion );
                if( !empty( $children ) ){
                    $cat['children'] = $children;
                }

            }
        }

        return $list;
    }

    public static function update2( $id, $data ){

        global $_W;
        $uniacid = current_uni_account();
        $sql = 'update sh_cms_category set '
            . ' `uniacid`=' . $uniacid
            . ' ,`parent_id`=' . $data['parent_id']
            . ' ,`name`="' . $data['name'] . '"'
//            . ' ,`path`="' . $data['path'] . '"'
            . ' ,`image`="' . $data['image'] . '"'
            . ' ,`sort_order`=' . $data['sort_order']
            . ' ,`is_recommend`=' . $data['is_recommend']
            . ' where id = ' . $id
        ;

        // return pdo_query( $sql );
        return Db::execute( $sql );
    }

    public static function delete2( $id ){
        // global $_W;
        $uniacid = current_uni_account();
        self::removeCategoryPath( $id );

        $sql = "delete from sh_cms_category where "
            . " uniacid= " . $uniacid
            . " and id=" . $id;
        // return pdo_query( $sql );
        return Db::execute( $sql );
    }
    public static function insert( $data ){

        // global $_W;
        $uniacid = current_uni_account();
        $sql = 'insert into sh_cms_category set '
            . ' `uniacid`=' . $uniacid
            . ' ,`parent_id`=' . $data['parent_id']
            . ' ,`name`="' . $data['name'] . '"'
//            . ' ,`path`="' . $data['path'] . '"'
            . ' ,`image`="' . $data['image'] . '"'
            . ' ,`sort_order`=' . $data['sort_order']
            . ' ,`is_recommend`=' . $data['is_recommend']
        ;

        // pdo_query( $sql );
        Db::execute( $sql );

        // return pdo_insertid();
        return Db::name('sh_cms_category')->getLastInsID();
    }

    // 获取分类所有的上级路径
    public static function getParentsPath( $id ){

        $path       = [];
        $path_ids   = [];
        $path_names = [];
        $current_id = $id;
        while( $current_id ){

            $category = self::single( $current_id );
            $path[] = $category;
            $path_ids[] = $current_id;
            $path_names[] = $category['name'];
            $current_id = $category['parent_id'];
        }

        $path       = array_reverse( $path );
        $path_ids   = array_reverse( $path_ids );
        $path_names = array_reverse( $path_names );
        return [
            'path'          => $path,
            'path_ids'      => $path_ids,
            'path_names'    => $path_names,
        ];
    }

    public static function removeCategoryPath( $id ){

        $sql = 'delete from sh_cms_category_path where category_id=' . $id;
        // pdo_query( $sql );
        Db::execute( $sql );

    }

    public static function setCategoryPath( $id, $parent_id ){

        self::removeCategoryPath( $id );
        $sql = 'select * from sh_cms_category_path where category_id=' . $parent_id;
        // $path = pdo_fetchall( $sql );
        $path = Db::query( $sql );

        $insert_sql = 'insert into sh_cms_category_path(category_id,path_id) values ';
        $value = [];

        foreach( $path as $item ){

            $value[] = '(' . $id .','. $item['path_id'] . ')';

        }

        $value[] = '(' . $id .','. $id . ')';

        $insert_sql .= implode(',', $value );

        // return pdo_query( $insert_sql );
        return Db::execute( $insert_sql );

    }


}